﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace 考勤数据处理软件
{
    public partial class MainForm : Form
    {
        DataTable dt;
        public MainForm()
        {
            InitializeComponent();
        }

        private void btn_open_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.InitialDirectory = @"C:\Users\code8086\Desktop\8月";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txt_filepath.Text = openFileDialog.FileName;
                Console.WriteLine(openFileDialog.FileName);
            }
        }

        private void btn_process_Click(object sender, EventArgs e)
        {
            if (txt_filepath.Text.Trim() == "")
            {
                MessageBox.Show("请选择文件！");
                return;
            }
            // ---- 处理文件
            XmlDocument doc = new XmlDocument();
            doc.Load(txt_filepath.Text);
            //doc.Load("../../all.xml");

            XmlElement root = null;
            root = doc.DocumentElement;

            XmlNodeList listNodes = null;
            listNodes = root.ChildNodes;
            
            foreach(XmlNode node in listNodes)
            {
                if (node.Name == "Worksheet")
                {
                    //Console.WriteLine("----------------------" + node.InnerText + "\n");
                    XmlNodeList c = node.ChildNodes;
                    XmlNode table = c[0];

                    XmlNodeList rows = table.ChildNodes;

                    // ---- 获取姓名和工号
                    XmlNode name_number = rows[3];
                    Console.WriteLine(name_number.ChildNodes[1].InnerText + " " + name_number.ChildNodes[3].InnerText);
                    DataRow dr = dt.NewRow();
                    dr["姓名"] = name_number.ChildNodes[1].InnerText;
                    dr["工号"] = Convert.ToInt32( name_number.ChildNodes[3].InnerText );
                    dr["标准时间"] = 164;
                    double total = 0.00;
                    for (int i = 11; i <=26; i++ )
                    {
                        total += Work.work_time_one(rows[i].ChildNodes[1].InnerText, rows[i].ChildNodes[2].InnerText);
                        total += Work.work_time_two(rows[i].ChildNodes[3].InnerText, rows[i].ChildNodes[4].InnerText);
                        total += Work.work_time_thr(rows[i].ChildNodes[5].InnerText, rows[i].ChildNodes[6].InnerText);

                        total += Work.work_time_one(rows[i].ChildNodes[10].InnerText, rows[i].ChildNodes[11].InnerText);
                        total += Work.work_time_two(rows[i].ChildNodes[12].InnerText, rows[i].ChildNodes[13].InnerText);
                        total += Work.work_time_thr(rows[i].ChildNodes[14].InnerText, rows[i].ChildNodes[15].InnerText);
                        

                    }
                    dr["工作时间"] = total;
                    dr["加班时间"] = total - Convert.ToDouble(dr["标准时间"]);
                    dt.Rows.Add(dr);
                    //foreach (XmlNode row in rows)
                    //{
                    //    if (row.Name == "Row")
                    //    {
                    //        XmlNodeList cells = row.ChildNodes;
                    //        for (int i = 0; i < cells.Count; i++)
                    //        {
                    //            XmlNode cell = cells[i];
                    //            Console.WriteLine(i + " - " + cell.InnerText);
                    //        }
                    //    }
                    //}
                    //break;
                }
                
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
            //txt_filepath.Text = @"C:\Users\code8086\Desktop\8月\all.xml";
            dt = new DataTable();
            dt.Columns.Add("工号", System.Type.GetType("System.Int32"));
            dt.Columns.Add("姓名", System.Type.GetType("System.String"));
            dt.Columns.Add("标准时间", System.Type.GetType("System.Double"));
            dt.Columns.Add("工作时间", System.Type.GetType("System.Double"));
            dt.Columns.Add("加班时间", System.Type.GetType("System.Double"));
            dataGridView1.DataSource = dt;
        }
    }
}
